<template>
  <div class="common_wrap">
    <div class="common_title">通知配置</div>
    <div class="btn-container" style="text-align: right">
      <el-button type="primary" icon="el-icon-plus" @click="toAdd"
        >新增</el-button
      >
    </div>
    <el-table ref="table" v-loading="tableLoading" :data="tableData">
      <el-table-column
        prop="content"
        label="通知内容"
        show-overflow-tooltip
        align="left"
      >
        <!-- <template slot-scope="scope">
          <div>
            <el-input
              ref="addNoticeInput"
              type="text"
              v-model="scope.row.content"
              v-if="scope.row.isNew"
              @blur="addNotice(scope.row.content)"
              maxlength="64"
            />
            <el-tooltip
              v-else
              class="item"
              effect="dark"
              :content="scope.row.content"
              placement="top"
            >
              <div
                style="
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                "
              >
                {{ scope.row.content }}
              </div>
            </el-tooltip>
          </div>
        </template> -->
      </el-table-column>
      <el-table-column prop="empList" label="提醒人员" align="left">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="formatList(scope.row.empList)"
            placement="top"
          >
            <div
              style="
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              "
            >
              {{ formatList(scope.row.empList) }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="scope">
          <!-- 跳转到编辑页面 -->
          <el-tooltip content="编辑" placement="top">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(scope.row)"
            />
          </el-tooltip>
          <!-- 设置提醒人 -->
          <!-- <el-button type="text" @click="setUpRemind(scope.row)"
            >设置提醒人</el-button
          > -->
          <!-- 发送 -->
          <el-tooltip content="发送" placement="top">
            <el-button
              type="primary"
              icon="el-icon-s-promotion"
              circle
              @click="send(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      :background="true"
      :current-page="queryParams.current"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </div>
</template>

<script>
export default {
  name: "NoticeSetting",
  data() {
    return {
      // 搜索条件
      queryParams: {
        current: 1, // 第几页
        size: 10, // 每页条数
      },
      total: 0, // 总条数
      tableLoading: false,
      tableData: [
        {
          id: "1",
          content: "通知测试1",
          empList: ["测试1", "测试2", "测试3", "测试4"],
        },
      ],
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    // 跳转到编辑页面
    edit(row) {
      console.log("edit=================", row);
      this.$router.push({
        path: "/noticeSetting/edit",
        query: {
          id: row.id,
        },
      });
    },
    // 设置提醒人
    // setUpRemind(row) {
    //   console.log("setUpRemind=================", row);
    // },
    // 发送
    send(row) {
      console.log("send=================", row);
      this.$api
        .noticeSend({
          content: row.content,
          emps: row.empList.map((v) => v.empId).join(","),
        })
        .then((res) => {
          if (res.data.errno === 200) {
            this.$message.success("发送成功");
          } else {
            this.$message.error("发送失败");
          }
        });
    },
    // 失焦触发新增请求
    addNotice(name) {
      console.log(name);
      this.tableData[0].isNew = false;
      // http add
      this.$api.noticeAdd({ content: name, emps: "" }).then((res) => {
        if (res.status === 200) {
          this.$message.success("成功新增");
          this.getTableData();
        }
      });
    },
    // 模拟新增
    toAdd() {
      this.$router.push({
        path: "/noticeSetting/edit",
      });
      // 数据结构待定
      // this.tableData.unshift({
      //   content: "",
      //   id: "",
      //   empList: [],
      //   isNew: true,
      // });

      // this.$nextTick(() => {
      //   this.$refs.addNoticeInput.focus();
      // });
    },
    getTableData() {
      this.tableLoading = true;
      const params = {
        page: this.queryParams.current,
        limit: this.queryParams.size,
      };
      this.$api.noticeList(params).then((res) => {
        console.log(res);
        this.tableLoading = false;
        if (res.status === 200) {
          this.tableData = res.data.items;
          this.total = res.data.total;
        } else {
          this.tableData = [];
          this.total = 0;
        }
      });
    },
    // 每页显示数量变化
    sizeChange(size) {
      this.queryParams.size = size;
      this.queryParams.current = 1;
      this.getTableData();
    },

    // 当前页变化
    currentChange(current) {
      this.queryParams.current = current;
      this.getTableData();
    },
    formatList(list) {
      return list.map((v) => v.empName).join("，");
    },
  },
};
</script>

<style scoped>
</style>